﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Douban.Core.Settings;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using Douban.Core;

namespace Douban
{
    public class SettingViewModel : ObservableObject, ISettingViewModel
    {
        private static double _minRefreshFrequency = 1;
        private static double _maxRefreshFrequency = 10;
        private ISettingManager _settingManager;
        private ICommand _closeCommand;
        private ICommand _saveCommand;
        private ICommand _cancelCommand;
        private SettingItems _settings;

        public SettingViewModel(ISettingView view, ISettingManager settingManager)
        {
            View = view;
            View.Context = this;

            _settingManager = settingManager;
            _settings = _settingManager.Settings;
        }

        public double MinRefreshFrequency
        {
            get { return _minRefreshFrequency; }
        }

        public double MaxRefreshFrequency
        {
            get { return _maxRefreshFrequency; }
        }

        public double RefreshFrequency
        {
            get { return _settings.RefreshFrequency == 0 ? MinRefreshFrequency : _settings.RefreshFrequency; }
            set
            {
                if (_settings.RefreshFrequency != value)
                {
                    _settings.RefreshFrequency = Math.Round(value * 10) / 10;
                    NotifyPropertyChanged("RefreshFrequency");
                }
            }
        }

        public string GetMessageHotKey
        {
            get 
            { 
                if(_settings.GetMessageHotKey != null) 
                    return _settings.GetMessageHotKey.ToString();
                return "";
            }
            set
            {
                HotKeySetting hotKeySetting = new HotKeySetting();
                hotKeySetting.Parse(value);
                _settings.GetMessageHotKey = hotKeySetting;
                NotifyPropertyChanged("GetMessageHotKey");
            }
        }	

        public SendMessageHotKey SendMessageHotKey
        {
            get { return _settings.SendMessageHotKey; }
            set
            {
                if (_settings.SendMessageHotKey != value)
                {
                    _settings.SendMessageHotKey = value;
                    NotifyPropertyChanged("SendMessageHotKey");
                }
            }
        }

        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new DelegateCommand<object>(obj => View.CloseView());

                return _closeCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            _settingManager.Save();
                            View.ShowSuccessMessage();
                            View.CloseView();
                        });

                return _saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                    _cancelCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            _settingManager.Undo();
                            View.CloseView();
                        });

                return _cancelCommand;
            }
        }

        #region ISettingViewModel Members

        public ISettingView View { get; set; }

        public void Open()
        {
            View.ShowView();
        }

        #endregion
    }
}
